package com.gs.SynAntiWandering.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.control.ToolBarSearch;
import com.gs.SynAntiWandering.control.ToolBarTitle;
import com.gs.SynAntiWandering.db.query.BaseQuery;
import com.gs.SynAntiWandering.presenter.BasePresenter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListFragment<V, T extends BasePresenter<V>, D, Q extends BaseQuery> extends Fragment {
    protected Context context;
    private View view;
    protected T mPresenter;
    protected List<D> mData;
    protected Q query;
    protected int totalPages;
    protected SmartRefreshLayout smartRefreshLayout;
    protected RecyclerView recyclerView;
    protected ToolBarSearch toolBarSearch;
    protected ToolBarTitle toolBarTitle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        context = getActivity();
        mPresenter = createPresenter();
        query = createQuery();
        mData = new ArrayList<>();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        recyclerView = fd(R.id.recyclerview);
        smartRefreshLayout = fd(R.id.refresh);
        if (smartRefreshLayout != null) {
            // 刷新
            smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
                smartRefreshLayout.setTag(AllConstants.REFRESH);
                mData.clear();
                if (query != null) {
                    query.setPageIndex(1);
                    loadData();
                }
            });
            // 加載更多
            smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
                smartRefreshLayout.setTag(AllConstants.LOAD_MORE);
                if (query != null) {
                    if (totalPages == 0) {
                        totalPages = 1;
                    }
                    if (totalPages <= query.getPageIndex()) {
                        query.setPageIndex(++totalPages);
                    } else {
                        query.setPageIndex(query.getPageIndex() + 1);
                    }
                    loadData();
                }
            });
        }
        toolBarTitle = fd(R.id.toolBarTitle);
        if (toolBarTitle != null) {
            toolBarTitle.setOnClickListener(v -> {
                if (v.getId() == R.id.item_per) {
                    filter();
                }
            });
        }
        toolBarSearch = fd(R.id.toolBarSearch);
        if (toolBarSearch != null) {
            toolBarSearch.setOnClickListener(v -> {
                if (v.getId() == R.id.item_per) {
                    search();
                }
            });
        }
        initView();
        search();
        return view;
    }

    protected abstract int getLayoutId();

    protected abstract T createPresenter();

    protected abstract Q createQuery();

    protected abstract void initView();

    protected abstract void loadData();

    protected abstract void search();

    public abstract void filter();

    /**
     * 更新SmartRefreshLayout狀態
     */
    public void refreshSmartRefreshLayout() {
        if (smartRefreshLayout.getTag() == null) {
            return;
        }
        if (smartRefreshLayout.getTag().equals(AllConstants.REFRESH)) {
            smartRefreshLayout.finishRefresh();
        } else {
            smartRefreshLayout.finishLoadMore();
        }
    }

    protected <I extends View> I fd(int id) {
        return view.findViewById(id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
